package mop.controller;

import java.util.List;

import mop.domain.user.UserPool;
import mop.exception.IllegalDuplicationException;
import mop.exception.IllegalNameException;
import mop.exception.IllegalUserException;

/**
 * The connection between GUI and domain.
 * Only knows built-in Java data types and strings.
 * 
 * @invar	Has no variables of its own, except for pool,
 * 			which is a reference to a static variable.
 *        
 * @author	MOP Team 7
 */
public class UserController 
{

	/**
	 * Creates a UserController with no extra settings.
	 * Constructor must be at protected level.
	 * Allows inheritance, but no out of package creation.
	 * 
	 * @post 	pool is set to UserPool.getInstance();
	 */
	protected UserController()
	{
		this.pool = UserPool.getInstance();
	}
	
	
	/***************************************************************************
	 * 							instance
	 ***************************************************************************/
	
	/**
	 * Always use this function to access UserController.
	 * 
	 * @effect 	UserController gets created - if it didn't exist yet.
	 *  		|instance = new UserController()
	 * 
	 * @return 	The static instance of UserController.
	 */
	public static UserController getInstance()
	{
		if(instance == null) 
		{
	         instance = new UserController();
	    }
		return instance;	
	}
	
	/**
	 * instance to self
	 */
	private static UserController instance = null;
	
	
	/***************************************************************************
	 * 							UserPool
	 ***************************************************************************/
	
	/**
	 * Return a list of all the Users.
	 * 
	 * @return	A list of user strings, from the pool.
	 */
	public List<String> getUserList()
	{
		return this.pool.getUserList();
	}
	
	/**
	 * Creates a new user.
	 * 
	 * @param 	name
	 * 			The name of the User you want to create.
	 * 
	 * @post 	A new User instance is created in the pool.
	 * 
	 * from our lovely pool it throws :
	 * @throws 	IllegalDuplicateException
	 * 			if the User is a duplicate
	 * 			|!isUniqueUser(name)
	 * 
	 * @throws 	IllegalNameException 
	 *       	if the name isn't a non empty string
     *       	|!isValidName(name)
	 */
	public void createUser(String name) 
		throws IllegalNameException, IllegalDuplicationException
	{
		this.pool.addUser(name);
	}
	
	/**
	 * Sets as current User.
	 * 
	 * @param 	name
	 *  		The name of the user you want to set as current.
	 *
	 * from the pool it throws :
	 * @throws 	IllegalUserException
	 * 			if the user already exists throw exception
	 * 			| hasUser(name)
	 */
	public void setCurrentUser(String name) 
		throws IllegalUserException
	{
		pool.setCurrentUser(name);
	}
	
	/**
	 * Returns the current User.
	 * 
	 * @return 	The current user stored in the pool. Returns null if none has been set.
	 */
	public String getCurrentUser()
	{
		return pool.getCurrentUser();
	}
	
	/**
	 * reference to UserPool.getInstance() just to avoid having to write that all the frakking time
	 */
	private UserPool pool;
	
}